package com.checkoutcheckins.client;

import com.checkoutcheckins.client.data.CheckoutcheckinsHomeContainer;
import com.checkoutcheckins.client.handlers.CheckoutCheckinsResizeHandler;
import com.checkoutcheckins.client.panels.CenterPanel;
import com.checkoutcheckins.client.panels.SpreadWordPopupPanel;
import com.checkoutcheckins.client.panels.WestPanel;
import com.checkoutcheckins.client.tasks.RetrivalTasks;
import com.checkoutcheckins.client.widgets.CheckoutCheckinsMapContainer;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;

public class CheckoutCheckinsHome implements EntryPoint, Home {

	private CheckoutcheckinsHomeContainer data = new CheckoutcheckinsHomeContainer(
			this);

	private WestPanel westPanel = new WestPanel();
	private CenterPanel centerPanel = new CenterPanel();

	private HorizontalPanel mainPanel = new HorizontalPanel();

	@Override
	public void onModuleLoad() {
//
//		Cookies.setCookie("4sqtoken",
//				"YREYVTBIC2DTZ4KHQS5TNUCWUTSUYJKLLYVM0DUTWQFDAU2P");
//		Cookies.setCookie("4sqsecret",
//				"QP51FOZKUYHNGEYV0JMW152IWVHAJEAKYOV4ISDWTFZ01EBO");

		mainPanel.setStyleName("dockPanel");
		mainPanel.setWidth("100%");

		mainPanel.add(westPanel);
		mainPanel.add(centerPanel);

		mainPanel.setCellWidth(westPanel, "300px");

		addMapContent();

		data.setStatusPanel(westPanel.addStatusPanel());

		final SpreadWordPopupPanel spreadWord = new SpreadWordPopupPanel();

		mainPanel.add(spreadWord);

		RootPanel.get("main").add(mainPanel);

		if (cookiesExist()) {
			RetrivalTasks.retrieveFoursquareUser(data.getToken(), data
					.getSecret(), data);

//			Timer t = new Timer() {
//
//				@Override
//				public void run() {
//					if (!data.getUserSettings().isWordSpreadAsked()) {
//						spreadWord.show(data.getFoursquareUser());
//					}
//				}
//			};
//
//			t.schedule(150000);

		} else {
			westPanel.addSigninWidget();
			westPanel.addFooter();
		}

		Window.addResizeHandler(new CheckoutCheckinsResizeHandler(data));

	}

	private void addMapContent() {
		CheckoutCheckinsMapContainer mapContainer = new CheckoutCheckinsMapContainer();
		data.setMapContainer(mapContainer);
		centerPanel.add(data.getMap());
	}

	public CheckoutcheckinsHomeContainer getData() {
		return data;
	}

	private boolean cookiesExist() {
		if (Cookies.getCookie("4sqtoken") != null
				&& Cookies.getCookie("4sqsecret") != null) {
			data.setSecret(Cookies.getCookie("4sqsecret"));
			data.setToken(Cookies.getCookie("4sqtoken"));
			return true;
		}
		return false;
	}

	public void addFoursquareUserToPage() {
		westPanel.addUserWidget(data.getFoursquareUser(), true);
		westPanel.addStackedPanel(data.getFoursquareUser());
	}

	@Override
	public void updateFoursquareUser() {
		addFoursquareUserToPage();

		// if not a shout alone, set center
		if (data.getFoursquareUser().getCheckin().getVenue() != null) {
			data.getMapContainer().setMapCenter(
					data.getFoursquareUser().getCheckin().getVenue()
							.getGeolat(),
					data.getFoursquareUser().getCheckin().getVenue()
							.getGeolong());
		}
	}

	@Override
	public void updateFoursquareCheckins() {
		data.getMapContainer().addCheckinsToMap(data.getMap(),
				data.getFoursquareCheckins(), data.getFoursquareUser());
		westPanel.updateStackedPanel(data.getFoursquareUser(), data
				.getFoursquareCheckins());
	}

	public HorizontalPanel getMainPanel() {
		return mainPanel;
	}

	public void setMainPanel(HorizontalPanel mainPanel) {
		this.mainPanel = mainPanel;
	}

	@Override
	public void updateFoursquareFriendCheckins() {
		data.getMapContainer().addFriendCheckinsToMap(data.getMap(),
				data.getFoursquareFriendCheckins());
	}
}
